/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.convert.math;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.jetbrains.annotations.NotNull;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import cn.seaboot.commons.core.NumberUtils;
import cn.seaboot.commons.lang.Warning;

/**
 * Converts from a BigDecimal any JDK-standard Number implementation.
 * Byte.valueOf()等一系列函数，作用是将基础类型转换为封装类型，
 * 看起来，数据转换会损失一部分的性能，实际上，在代码传参之后，基础类型会转换为封装类型，
 * 不需要尝试优化这一类的相关代码。
 *
 * @author Mr.css
 * @version 2020-11-24 11:42
 * @see Byte
 * @see Short
 * @see Integer
 * @see Long
 * @see java.math.BigInteger
 * @see Float
 * @see Double
 * @see java.math.BigDecimal
 * @see NumberUtils
 */
public final class BigDecimalToNumberConverterFactory implements ConverterFactory<BigDecimal, Number> {

    @NotNull
    @Override
    public <T extends Number>  Converter<BigDecimal, T> getConverter(@NotNull Class<T> targetType) {
        return new BigDecimalToNumber<>(targetType);
    }

    private static final class BigDecimalToNumber<T extends Number> implements Converter<BigDecimal, T> {

        private final Class<T> targetClass;

        BigDecimalToNumber(Class<T> targetType) {
            this.targetClass = targetType;
        }

        @Override
        @SuppressWarnings(Warning.UNCHECKED)
        public T convert(@NotNull BigDecimal number) {
            if (Byte.class == targetClass) {
                return (T) Byte.valueOf(number.byteValueExact());
            } else if (Short.class == targetClass) {
                return (T) Short.valueOf(number.shortValueExact());
            } else if (Integer.class == targetClass) {
                return (T) Integer.valueOf(number.intValueExact());
            } else if (Long.class == targetClass) {
                return (T) Long.valueOf(number.longValueExact());
            } else if (BigInteger.class == targetClass) {
                return (T) number.toBigInteger();
            } else if (Float.class == targetClass) {
                return (T) Float.valueOf(number.floatValue());
            } else if (Double.class == targetClass) {
                return (T) Double.valueOf(number.doubleValue());
            }
            throw new IllegalArgumentException("Cannot convert String [" + number + "] to target class [" + targetClass.getName() + "]");
        }
    }
}
